package com.avic.rong.adapter.collect;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.activity.mine.CollectionActivity;
import com.avic.rong.api.CollectThumbNumApi;
import com.avic.rong.api.collect.ZanCancelApi;
import com.avic.rong.bean.collect.QuestAnswerBean;
import com.avic.rong.questionOrAnswer.activity.AnswerActivity;
import com.avic.rong.questionOrAnswer.activity.AnswerDetailsActivity;
import com.avic.rong.questionOrAnswer.activity.QuestionDetailsActivity;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.utils.ToastTools;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;
import com.mcxtzhang.swipemenulib.SwipeMenuLayout;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class QuestAnswerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private ArrayList<QuestAnswerBean> mList;

    public void setmList(ArrayList<QuestAnswerBean> mList) {
        this.mList = mList;
    }

    public QuestAnswerAdapter(Context mContext, ArrayList<QuestAnswerBean> mList, String token) {
        this.mContext = mContext;
        this.mList = mList;
        this.token = token;
    }

    private String token;


    private int   TYPE_QUEST = 1;
    private int TYPE_ANSWER = 2;

    public void setData(List<QuestAnswerBean> list){
        mList.addAll(list);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        RecyclerView.ViewHolder holder = null;

        if (i == TYPE_QUEST){
            View inflate = LayoutInflater.from(mContext).inflate(R.layout.item_anwer, viewGroup, false);
            holder = new AnswerHolder(inflate);
        }else if (i == TYPE_ANSWER){
            View inflate = LayoutInflater.from(mContext).inflate(R.layout.item_quest, viewGroup, false);
            holder = new QuoestHolder(inflate);
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        QuestAnswerBean bean = mList.get(i);
        long date = bean.getPublicTime();
        int itemViewType = getItemViewType(i);
        if (itemViewType == TYPE_QUEST){//提问
            AnswerHolder answerHolder = (AnswerHolder) viewHolder;
            int isV = bean.getIsV();
            if (isV == 1){
                answerHolder.ivVip.setVisibility(View.VISIBLE);
            }else {
                answerHolder.ivVip.setVisibility(View.GONE);
            }
            answerHolder.tv_date.setText(longToDate(date));
           // answerHolder.tv_date.setText(date.substring(0,10));
            answerHolder.tv_question.setText(bean.getTitle());

            answerHolder.tv_desc.setText(bean.getAuthenInfo());

            RequestOptions requestOptions = new RequestOptions().error(R.mipmap.icon_message_photo).circleCrop();
            Glide.with(mContext).applyDefaultRequestOptions(requestOptions).load(bean.getLogo()).error(R.mipmap.ic_default_head).into(answerHolder.image_header);
            answerHolder.tv_name.setText(bean.getUser_name());
            //侧滑删除
            answerHolder.btn_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    /*if (questClick!=null){
                        questClick.onClick(bean,i);
                    }*/
                    answerHolder.finalView.quickClose();
                    if (questAnswerButtonClick!=null){
                        questAnswerButtonClick.onClick(bean,i,13,true);
                    }
                }
            });
            answerHolder.textview_answer.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, AnswerActivity.class);
                    intent.putExtra("qesID",bean.getId()+"");
                    mContext.startActivity(intent);
                }
            });
            answerHolder.llAnswer.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, QuestionDetailsActivity.class);
                    intent.putExtra("queID",bean.getId()+"");
                    mContext.startActivity(intent);
                }
            });


        }else if (itemViewType ==TYPE_ANSWER){//回答
            QuoestHolder quoestHolder = (QuoestHolder) viewHolder;
            int isV = bean.getIsV();
            if (isV == 1){
                quoestHolder.ivVip.setVisibility(View.VISIBLE);
            }else {
                quoestHolder.ivVip.setVisibility(View.GONE);
            }
            boolean thumb = bean.isThumb();
            quoestHolder.cb_zan.setChecked(thumb);
            boolean favorite = bean.isFavorite();
            quoestHolder.cb_collect.setChecked(favorite);

            quoestHolder.tv_date.setText(longToDate(date));
            //quoestHolder.tv_date.setText(date.substring(0,10));
            quoestHolder.tv_collect.setText(bean.getCollectNum()+"");//收藏量
            //quoestHolder.tv_toview.setText(bean.getViewNum()+"");//观看量
            quoestHolder.tv_zan.setText(bean.getNiceNum()+"");//点赞量
            quoestHolder.tv_comment.setText(bean.getAppraiseNum()+"");//评论量

            quoestHolder.tv_question.setText(bean.getTitle());

            quoestHolder.tv_info.setText(bean.getAuthenInfo());

            quoestHolder.tv_answer.setText(bean.getContent());



            quoestHolder.tv_answer.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                @Override
                public boolean onPreDraw() {
                    int lineCount = quoestHolder.tv_answer.getLineCount();
                    if (lineCount < 3){
                        quoestHolder.tv_quanwen.setVisibility(View.GONE);
                    }else {
                        quoestHolder.tv_quanwen.setVisibility(View.VISIBLE);
                    }
                    quoestHolder.tv_answer.getViewTreeObserver().removeOnPreDrawListener(this);
                    return false;
                }
            });


            quoestHolder.tv_quanwen.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, AnswerDetailsActivity.class);
                    intent.putExtra("ansID",bean.getId()+"");
                    mContext.startActivity(intent);
                }
            });




            RequestOptions requestOptions = new RequestOptions().error(R.mipmap.icon_message_photo).circleCrop();
            Glide.with(mContext).applyDefaultRequestOptions(requestOptions).load(bean.getLogo()).error(R.mipmap.ic_default_head).into(quoestHolder.image_header);
            quoestHolder.tv_name.setText(bean.getUser_name());
            quoestHolder.tv_question.setText(bean.getTitle());
            //点赞
            quoestHolder.cb_zan.setOnClickListener(new MyCheckedListener(quoestHolder,bean));
            //点击收藏
            quoestHolder.cb_collect.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String trim = quoestHolder.tv_collect.getText().toString().trim();
                    Integer integer = Integer.valueOf(trim);

                    if (bean.isFavorite()){
                        quoestHolder.tv_collect.setText((integer-1)+"");
                        bean.setFavorite(false);
                    }else {
                        quoestHolder.tv_collect.setText((integer+1)+"");
                        bean.setFavorite(true);
                    }
                    if (questAnswerButtonClick!=null){
                        questAnswerButtonClick.onClick(bean,i,14,false);
                    }
                }
            });

            //侧滑删除
            quoestHolder.btn_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    quoestHolder.finalView.quickClose();
                    if (questAnswerButtonClick!=null){
                        questAnswerButtonClick.onClick(bean,i,14,true);
                    }
                }
            });


            quoestHolder.llQuest.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, AnswerDetailsActivity.class);
                    intent.putExtra("ansID",bean.getId()+"");
                    mContext.startActivity(intent);
                }
            });

        }
    }

    public static String longToDate(long lo){
        Date date = new Date(lo);
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
        return sd.format(date);
    }


    //点赞
    public class MyCheckedListener implements View.OnClickListener{

        QuoestHolder quoestHolder;
        QuestAnswerBean bean;


        public MyCheckedListener(QuoestHolder quoestHolder,    QuestAnswerBean bean) {
            this.quoestHolder = quoestHolder;
            this.bean = bean;
        }

        @Override
        public void onClick(View v) {
            ZanCancelApi zanCancelApi = new ZanCancelApi((CollectionActivity) mContext,new MyHttpOnNextListener(quoestHolder,bean));
            zanCancelApi.setArgument(bean.getId(),3,token);
            HttpManager.getInstance().doHttpDeal(zanCancelApi);
        }
    }
    //点赞
    public class  MyHttpOnNextListener extends   HttpOnNextListener<BaseResultEntity> {

        QuoestHolder quoestHolder;
        QuestAnswerBean bean;

        public MyHttpOnNextListener(QuoestHolder quoestHolder, QuestAnswerBean bean) {
            this.quoestHolder = quoestHolder;
            this.bean = bean;
        }

        @Override
        public void onNext(BaseResultEntity baseResultEntity) {
            if (baseResultEntity.getCode().equals(ConstantsUtils.STATUS_SUCCESS_200)){
                boolean checked = quoestHolder.cb_zan.isChecked();
                bean.setThumb(checked);

                String trim = quoestHolder.tv_zan.getText().toString().trim();
                Integer integer = Integer.valueOf(trim);
                if (checked){
                    ToastTools.showPublicToast("点赞成功");
                    integer = integer+1;
                }else {
                    ToastTools.showPublicToast("取消点赞");
                    integer = integer-1;
                }
                quoestHolder.tv_zan.setText(integer+"");

                CollectThumbNumApi collectThumbNumApi = new CollectThumbNumApi((CollectionActivity) mContext, new MyZanListener());
                collectThumbNumApi.setArgument("/answer/thumbNum",bean.getId());
                HttpManager.getInstance().doHttpDeal(collectThumbNumApi);

            }else {

            }
        }
    };

    //点赞数变动
    public class MyZanListener extends HttpOnNextListener<BaseResultEntity>{

        @Override
        public void onNext(BaseResultEntity baseResultEntity) {
           /* Toast toast = Toast.makeText(mContext, baseResultEntity.getCode() + "  " + baseResultEntity.getMsg(), Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER,0,0);
            toast.show();*/
        }
    }

    @Override
    public int getItemViewType(int position) {
        int type = mList.get(position).getType();
        if (type==2){
            return TYPE_ANSWER;
        }else if (type == 1) {
            return TYPE_QUEST;
        }
        return 0;
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class QuoestHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_vip)
        ImageView ivVip;
        @BindView(R.id.tv_date)
        TextView tv_date;
        @BindView(R.id.tv)
        TextView tv_name;
       /* @BindView(R.id.tv_toview)
        TextView tv_toview;
        @BindView(R.id.img_toview)
        ImageView img_toview;*/
        @BindView(R.id.image_header)
        ImageView image_header;
        @BindView(R.id.tv_collect)
        TextView tv_collect;
        @BindView(R.id.tv_zan)
        TextView tv_zan;
        @BindView(R.id.tv_comment)
        TextView tv_comment;
        @BindView(R.id.cb_collect)
        CheckBox cb_collect;
        @BindView(R.id.cb_zan)
        CheckBox cb_zan;
        @BindView(R.id.img_comment)
        ImageView img_comment;
        @BindView(R.id.tv_question)
        TextView tv_question;
        @BindView(R.id.tv_info)
        TextView tv_info;
        @BindView(R.id.tv_answer)
        TextView tv_answer;
        @BindView(R.id.tv_quanwen)
        TextView tv_quanwen;
        @BindView(R.id.collect_btn_delete)
        Button btn_delete;
        @BindView(R.id.linearlayout_quest)
        LinearLayout llQuest;
        @BindView(R.id.finalView)
        SwipeMenuLayout finalView;
        public QuoestHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
    class AnswerHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_vip)
        ImageView ivVip;
        @BindView(R.id.tv)
        TextView tv_name;
        @BindView(R.id.tv_date)
        TextView tv_date;
        @BindView(R.id.image_header)
        ImageView image_header;
        @BindView(R.id.tv_question)
        TextView tv_question;
        @BindView(R.id.textview_answer)
        TextView textview_answer;
        @BindView(R.id.tv_desc)
        TextView tv_desc;
        @BindView(R.id.collect_btn_delete)
        Button btn_delete;
        @BindView(R.id.finalView)
        SwipeMenuLayout finalView;
        @BindView(R.id.linearlayout_answer)
        LinearLayout llAnswer;
        public AnswerHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }

    private QuestAnswerButtonClick questAnswerButtonClick;

    public void setQuestAnswerButtonClick(QuestAnswerButtonClick questAnswerButtonClick) {
        this.questAnswerButtonClick = questAnswerButtonClick;
    }

    public interface QuestAnswerButtonClick{
        void onClick(QuestAnswerBean bean, int position,int type,boolean isRefresh);
    }

    public interface QuestClick{
        void onClick(QuestAnswerBean bean,int position);
    }

    public QuestClick questClick;

    public void setQuestClick(QuestClick questClick) {
        this.questClick = questClick;
    }
}
